<template>
  <el-container style="height: 100%">
    <el-header height="60px">
      <img src="@/assets/images/logo.png" alt="" />
      <el-menu
        class="el-menu-demo"
        mode="horizontal"
        background-color="#23262e"
        text-color="#fff"
        active-text-color="skyblue"
        router
      >
        <el-submenu index="1">
          <template slot="title">
            <img
              class="avatar"
              :src="$store.state.user.user.user_pic"
              v-if="$store.state.user.user.user_pic"
            />
            <img class="avatar" src="@/assets/logo.png" v-else /> 个人中心</template
          >
          <el-menu-item index="/user-info"><i class="el-icon-edit"></i>基本资料</el-menu-item>
          <el-menu-item index="/user-avatar"><i class="el-icon-edit"></i>更改头像</el-menu-item>
          <el-menu-item index="/user-pwd"><i class="el-icon-edit"></i>重置密码</el-menu-item>
        </el-submenu>
        <el-menu-item @click="lyout"
          ><a href="javascript:;"><i class="el-icon-switch-button"></i>退出</a></el-menu-item
        >
      </el-menu>
    </el-header>
    <el-container>
      <el-aside width="200px">
        <div class="welcome">
          <img
            class="avatar"
            :src="$store.state.user.user.user_pic"
            v-if="$store.state.user.user.user_pic"
          />
          <img class="avatar" src="@/assets/logo.png" v-else />
          欢迎您&nbsp;&nbsp;<span>{{
            $store.state.user.user.nickname || $store.state.user.user.username
          }}</span>
        </div>
        <el-menu
          default-active="2"
          class="el-menu-vertical-demo"
          background-color="#23262e"
          text-color="#fff"
          active-text-color="#ffd04b"
          unique-opened
          router
        >
          <template v-for="item in menu">
            <el-menu-item
              :key="item.indexPath"
              :index="item.indexPath"
              v-if="item.children === null"
            >
              <i :class="item.icon"></i>
              <span slot="title">{{ item.title }}</span>
            </el-menu-item>

            <el-submenu :key="item.indexPath" :index="item.indexPath" v-else>
              <template slot="title">
                <i :class="item.icon"></i>
                <span>{{ item.title }}</span>
              </template>
              <el-menu-item-group>
                <el-menu-item
                  :index="child.indexPath"
                  v-for="child in item.children"
                  :key="child.indexPath"
                  ><i :class="child.icon"></i>{{ child.title }}</el-menu-item
                >
              </el-menu-item-group>
            </el-submenu>
          </template>
        </el-menu>
      </el-aside>
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
import { getMenuAPI } from '@/api/menu'
export default {
  name: 'MyHome',
  data() {
    return {
      menu: []
    }
  },
  methods: {
    // 退出方法
    lyout() {
      this.$confirm('你确定要退出嘛? 铁子', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          this.$store.commit('user/updateToken', '')
          this.$store.commit('user/getUser', {})
          this.$router.push('/login')
        })
        .catch(() => {})
    },
    // 获取菜单数据
    async getMenu() {
      const { data: res } = await getMenuAPI()
      if (res.code === 0) {
        this.menu = res.data
      }
    }
  },
  created() {
    this.$store.dispatch('user/getUserInfo')
    this.getMenu()
  }
}
</script>

<style lang="less" scoped>
.el-header {
  background-color: #23262e;
  color: #fff;
  display: flex;
  justify-content: space-between;
  .el-avatar {
    margin-right: 15px;
  }
  a {
    text-decoration: none;
  }
}
.avatar {
  height: 35px;
  width: 35px;
  border-radius: 50%;
  // 下面的样式是，让图片按照原比例，完整的展示
  object-fit: cover;
  background-color: #fff;
  margin-right: 10px;
}
.welcome {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 70px;
}
.el-aside {
  background-color: #23262e;
  color: #fff;
}

.el-main {
  background-color: #f5f5f5;
}
</style>
